#include "../../inc/skyTimer.h"

#ifdef _SYMBIAN

namespace Sky
{
//typedef i32 (*aFunction)(TAny* aPtr);
Bool Timer::CreateTimer(i32 MicroSeconds,TimerCallback cb,Bool bRepeat )
{
	if(!m_pTimer)
	{
		m_pTimer = CPeriodic::NewL(CActive::EPriorityHigh);

		m_pTimer->Start(MicroSeconds, MicroSeconds, TCallBack(Timer::Run,this));

	}
//	CallbackNode node;
//	node._bRepeat = bRepeat;
//	node._cb = cb;
//	node._interal = MicroSeconds;
//	node._left = MicroSeconds;
//	_list.append(node);
	_cb = cb;
	return vTrue;
}

Void Timer::onTimer()
{
	_cb();
//	CBList::iterator itr;
//	const CallbackNode* node;
//	CallbackNode* pNode;
//	for(itr = _list.getHead(); itr != vNull; ++itr)
//	{
//		node = itr;
//		pNode = const_cast<CallbackNode*>(node);
//		pNode->_left --;
//		if(pNode->_left == 0)
//		{
//			(pNode->_cb)();
//			if(pNode->_bRepeat)
//				pNode->_left = pNode->_interal;
//			else
//				_list.remove(itr);
//		}
//	}
}
i32 Timer::Run( void * aPtr )
{
	((Timer*)aPtr )->onTimer();
	return 0;
}

Void Timer::DeleteTimer()
{
//	CBList::iterator itr;
//	const CallbackNode* node;
//	CallbackNode* pNode;
//	for(itr = _list.getHead(); itr != vNull;)
//	{
//		node = itr;
//		pNode = const_cast<CallbackNode*>(node);
//		if(pNode->_cb == callback)
//		{
//			_list.remove(itr);
//		}
//	}
	if(m_pTimer)
	{
		m_pTimer->Cancel();
		delete m_pTimer;
		m_pTimer = vNull;
	}
	return;
}

OSTimer::~OSTimer()
{
	
}
Timer::~Timer()
{
	if(m_pTimer)
	{
		m_pTimer->Cancel();
		delete m_pTimer;
		m_pTimer = vNull;
	}
}
}
#endif
